﻿#include "FITKNastranAdaptorMaterial.h"
#include "FITKNastranBDFIOTool.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKNasMaterialMAT1.h"

namespace Nastran
{
    bool FITKNastranAdaptorMaterial::adaptR()
    {
        if (!_reader) return false;
        //后退一行
        _reader->backLine();
        //获取读取工具和网格数据
        FITKNastranBDFIOReaderTool* readTool = _reader->getReaderTool();
        if (!readTool) return false;
        //获取数据
        QStringList data = readTool->readNastranBDFLineSpliter();
        if (data.isEmpty()) return false;
        QString materialType = data.at(0).toUpper();
        //读入材料
        bool readOK = false;
        if (materialType.startsWith("MAT1"))
            readOK = this->readMAT1(data);
        return true;
    }

    bool FITKNastranAdaptorMaterial::adaptW()
    {
        //获取数据对象
        Interface::FITKAbaMaterial* materialPtr = dynamic_cast<Interface::FITKAbaMaterial*>(_dataObj);
        if (!materialPtr) return false;
        
        return this->writeMAT1();
    }

    bool FITKNastranAdaptorMaterial::readMAT1(QStringList data)
    {
        Interface::FITKAbaMaterial* materialPtr = dynamic_cast<Interface::FITKAbaMaterial*>(_dataObj);
        if (data.size() < 3 || !materialPtr || !_reader) return false;
        //设置名称
        materialPtr->setDataObjectName("MAT1-1");
        //获取数据
        FITKNastranBDFIOReaderTool* readTool = _reader->getReaderTool();
        if (!readTool) return false;
        bool ok = false;
        //mID Integer>0
        int mID = data.at(1).trimmed().toInt(&ok);
        if (!ok) return false;
        //杨氏模量 Real>=0.0 or blank
        double youngMod = 0.0;
        if (data.size() >= 3 && !data.at(2).trimmed().isEmpty())
            youngMod = readTool->stringToDouble(data.at(2), ok);
        //剪贴模量 Real>=0.0 or blank
        double shearMod = 0.0;
        if (data.size() >= 4 && !data.at(3).trimmed().isEmpty())
            shearMod = readTool->stringToDouble(data.at(3), ok);
        //泊松比 -1.0<Real<=0.5 or blank
        double poisson = 0.0;
        if (data.size() >= 5 && !data.at(4).trimmed().isEmpty())
            poisson = readTool->stringToDouble(data.at(4), ok);
        //质量密度 Real
        double density = 0.0;
        if (data.size() >= 6 && !data.at(5).trimmed().isEmpty())
            density = readTool->stringToDouble(data.at(5), ok);
        //热膨胀系数 Real
        double thermalExpansion = 0.0;
        if (data.size() >= 7 && !data.at(6).trimmed().isEmpty())
            thermalExpansion = readTool->stringToDouble(data.at(6), ok);
        //参考温度 Real, (Default=0.0热膨胀系数被指定）
        double temperature = 0.0;
        if (data.size() >= 8 && !data.at(7).trimmed().isEmpty())
            temperature = readTool->stringToDouble(data.at(7), ok);
        //阻尼系数 Real
        double damping = 0.0;
        if (data.size() >= 9 && !data.at(8).trimmed().isEmpty())
            damping = readTool->stringToDouble(data.at(8), ok);
        //拉伸应力极限 Real>=0.0 or blank
        double tension = 0.0;
        if (data.size() >= 10 && !data.at(9).trimmed().isEmpty())
            tension = readTool->stringToDouble(data.at(9), ok);
        //压缩应力极限 Real>=0.0 or blank
        double compression = 0.0;
        if (data.size() >= 11 && !data.at(10).trimmed().isEmpty())
            compression = readTool->stringToDouble(data.at(10), ok);
        //剪贴应力极限 Real>=0.0 or blank
        double shear = 0.0;
        if (data.size() >= 12 && !data.at(11).trimmed().isEmpty())
            shear = readTool->stringToDouble(data.at(11), ok);
        //材料坐标ID Integer>=0 or blank
        int coordinateSysID = 0;
        if (data.size() >= 13 && !data.at(12).trimmed().isEmpty())
            coordinateSysID = data.at(12).trimmed().toInt(&ok);
        //添加动作
        materialPtr->addMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::NasMAT1);
        Interface::FITKNasMaterialMAT1* mat1 = dynamic_cast<Interface::FITKNasMaterialMAT1*>(materialPtr->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::NasMAT1));
        if (!mat1) return false;
        mat1->setYoungsModulus(youngMod);
        mat1->setShearModules(shearMod);
        mat1->setPoissionRatio(poisson);
        mat1->setDensity(density);
        mat1->setThermalExpCoeff(thermalExpansion);
        mat1->setRefTemp(temperature);
        mat1->setDampingCoeff(damping);
        mat1->setStressLimitTension(tension);
        mat1->setStressLimitCompress(compression);
        mat1->setStressLimitShear(shear);
        mat1->setCoordinateSys(coordinateSysID);
        //设置数据到映射表
        readTool->insertDataMapping(FITKNastranBDFIOReaderTool::ReadMappingType::ReadMIDMaterial, mID, materialPtr->getDataObjectID());
        return true;
    }

    bool FITKNastranAdaptorMaterial::writeMAT1()
    {
        if (!_writer)return false;
        //获取写出工具
        FITKNastranBDFIOWriterTool* writeTool = _writer->getWriterTool();
        if (!writeTool) return false;
        //获取数据对象
        Interface::FITKAbaMaterial* materialPtr = dynamic_cast<Interface::FITKAbaMaterial*>(_dataObj);
        if (!materialPtr) return false;
        Interface::FITKNasMaterialMAT1* mat1 = dynamic_cast<Interface::FITKNasMaterialMAT1*>(materialPtr->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::NasMAT1));
        if (!mat1) return false;
        QStringList data;
        data.append("MAT1");
        data.append(QString::number(materialPtr->getDataObjectID()));
        //获取数据
        bool ok = false;
        data.append(writeTool->doubleToString(mat1->getYoungsModulus(), ok));
        data.append(writeTool->doubleToString(mat1->getShearModules(), ok));
        data.append(writeTool->doubleToString(mat1->getPoissionRatio(), ok));
        data.append(writeTool->doubleToString(mat1->getDensity(), ok));
        data.append(writeTool->doubleToString(mat1->getThermalExpCoeff(), ok));
        data.append(writeTool->doubleToString(mat1->getRefTemp(), ok));
        data.append(writeTool->doubleToString(mat1->getDampingCoeff(), ok));
        data.append(writeTool->doubleToString(mat1->getStressLimitTension(), ok));
        data.append(writeTool->doubleToString(mat1->getStressLimitCompress(), ok));
        data.append(writeTool->doubleToString(mat1->getStressLimitShear(), ok));
        data.append(writeTool->doubleToString(mat1->getCoordinateSys(), ok));
        //写出数据
        writeTool->writeData(data);
        return true;
    }
} 